﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Pavilion.Locks;
using System.ComponentModel;
using Pavilion;
using Pavilion.DesignerTool.ViewTools;
using System.Collections;
using Pavilion.DesignerTool.ViewTools.ConnectableListBox;
using Pavilion.DesignerTool.NodeDesignerTool.ImplementationsForConnectableListbox;
using System.Collections.ObjectModel;

namespace Pavilion.DesignerTool
{
    /// <summary>
    /// Interaction logic for LockEditorView.xaml
    /// </summary>
    public partial class LockEditorView : UserControl
    {
        public LockEditorViewModel ViewModel { get; set; }

        public LockEditorView()
        {
            InitializeComponent();

            if (!DesignerProperties.GetIsInDesignMode(this))
                DataContext = ViewModel = new LockEditorViewModel();
        }

        public void SetLink(LinkWrapper linkWrapper)
        {
            ViewModel.LinkWrapper = linkWrapper;
        }

        private void AddMenuItem_Click(object sender, RoutedEventArgs e)
        {
            if (!((e.OriginalSource as MenuItem).DataContext is Type))
                return;

            ILock lockItem = (ILock)Activator.CreateInstance((e.OriginalSource as MenuItem).DataContext as Type);
            ViewModel.AddLockWrapper(new LockWrapper(lockItem));
        }

        private void RemoveMenuItem_Click(object sender, RoutedEventArgs e)
        {
            PropertyCategory propertyCategory = (sender as FrameworkElement).DataContext as PropertyCategory;
            ViewModel.RemoveLockWrapper(propertyCategory.AssociatedItem as ILock);
        }
    }
}

public class LockEditorViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public LinkWrapper LinkWrapper { get { return linkWrapper; } set { SetLinkWrapper(value); } }
    public NotifiableCollection<Type> LockTypeCollection { get; private set; }
    public IEnumerable LockCollection { get { return lockCollection; } private set { lockCollection = value; OnPropertyChanged("LockCollection"); } }

    private IEnumerable<Type> lockTypesHodler;
    private LinkWrapper linkWrapper;
    private IEnumerable lockCollection;

    public LockEditorViewModel()
    {
        lockTypesHodler = GameEngine.ObtainAllTypes<ILock>("Pavilion.Locks.dll");
        LockTypeCollection = new NotifiableCollection<Type>(lockTypesHodler);
    }

    internal void AddLockWrapper(LockWrapper lockWrapper)
    {
        linkWrapper.AddLockWrapper(lockWrapper);

        LockCollection = null;
        LockCollection = linkWrapper.Link.LockCollection;
    }

    internal void RemoveLockWrapper(ILock lockItem)
    {
        LinkWrapper.RemoveLock(lockItem);

        LockCollection = null;
        LockCollection = linkWrapper.Link.LockCollection;
    }

    private void SetLinkWrapper(LinkWrapper value)
    {
        linkWrapper = value;
        OnPropertyChanged("LinkWrapper");

        if (linkWrapper == null)
            LockCollection = null;
        else
            LockCollection = linkWrapper.Link.LockCollection;
    }

    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}
